<?php

function doMsg($msg,$username,$password) {
	
	$url = 'http://twitter.com/statuses/update.xml'; //Sends a message
	
	$curl_handle = curl_init();
	
	curl_setopt($curl_handle, CURLOPT_URL, "$url");
	curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
	curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl_handle, CURLOPT_POST, 1);
	curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");	
	
	
	curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$msg");
	
	$buffer = curl_exec($curl_handle);
	curl_close($curl_handle);
	
	// check for success or failure
	if (strpos($buffer,"error")>0) {
	    return false;
	} else {
	    return true;
	}	
}


function showError() {
	//where they came from
	$ref=@$HTTP_REFERER;
	echo '<div align="center" style="font-family:Arial; font-size;8pt; margin:50px auto; padding:10px; border:1px dotted GRAY; width:800px; background-color:#FFFFCC;">';
	echo '	<strong>There was an error sending the form,<br/>please check your username and password</strong><br/><br/><a href="javascript:history.back(1)">Go Back</a>';
	echo '</div>';
}

$msg = $_POST['twitter_message']; $msg = str_replace("\'","'",$msg);
$username = $_POST['twitter_username'];
$password = $_POST['twitter_password'];
$redirect = $_POST['twitter_redirect'];


if($msg!="" && $username!="" && $password!="" && $redirect!="") {
	if(doMsg($msg,$username,$password)) {
		header( "HTTP/1.1 301 Moved Permanently" ); 
		header( "Location: $redirect"); 
	} else {
		showError();
	}
} else {
	showError();
}

?>


